/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package server;


import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import java.util.Iterator;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 *
 * @author Dell 1564
 */
public final class chatInterfaceImplement extends UnicastRemoteObject implements chatInterface {
    private final int port = 1098;
    private final String server = "localhost";
    private Context context = null;
    
    private ServerList serverList = new ServerList();
    
    public chatInterfaceImplement() throws RemoteException {
         try{
            java.rmi.registry.LocateRegistry.createRegistry(port);
            System.out.println("Server is stop");
            runServer();
        }
        catch(Exception e1){
            e1.printStackTrace();
        }
    }

    @Override
    public synchronized void join (Notify n) throws RemoteException{
        serverList.add(n);
        serverList.incCounter();
        for(Iterator i = serverList.getCollection().iterator();
        i.hasNext();){
            Notify client = (Notify)i.next();
            client.joinMessage(n.getName());
        }
        serverList.decCounter();
    }
    @Override
    public synchronized void talk(Notify n, String s)
        throws RemoteException {
        serverList.incCounter();
        for (Iterator i = serverList.getCollection().iterator();
                        i.hasNext();){
            Notify client = (Notify)i.next();
            client.sendMessage(n.getName(),s);
        }
        serverList.decCounter();
    }

    @Override
    public synchronized void leave(Notify n) throws RemoteException{
        serverList.remove(n);
        serverList.incCounter();
        for (Iterator i = serverList.getCollection().iterator();
                               i.hasNext();){
                       Notify client =  (Notify)i.next();
                       client.exitMessage(n.getName());
        }
        serverList.decCounter();
    }
    public void runServer(){
        Thread t = new Thread(new Runnable() {
            @Override
             public void run()
                    {
                       try {
                        chatInterfaceImplement chat = new chatInterfaceImplement();
                        context = new InitialContext();
                        context.rebind("rmi://"+server+":"+port+"/chatrmi", chat);
                          
                         System.out.println("Chat server is running");
                 
                       }catch (RemoteException e){
                            System.out.println("Communication Error" + e.toString());
                       }catch (Exception ex) {
                            ex.printStackTrace();
                       }
                    }
     
        });
        t.start();
    }
    public static void main(String[] args) throws RemoteException{
       new chatInterfaceImplement();
    }
}

